// 用户相关类型
export interface IUser {
  _id?: string;
  username: string;
  email: string;
  password: string;
  role: UserRole;
  nickname?: string;
  defaultVehicleId?: string;
  createdAt?: Date;
  updatedAt?: Date;
}

export enum UserRole {
  GUEST = 'Guest',
  USER = 'User',
  VIP = 'VIP',
  ADMIN = 'Admin',
  SUPER_ADMIN = 'SuperAdmin',
}

// 车辆相关类型
export interface TCar {
  id: string;
  // 昵称
  name: string;
  // 类型 (有：小汽车，摩托车，卡车，电车，其他)
  category: string;
  // 品牌
  brand: string;
  // 模式
  model: string;
  // 生产时间 （时间戳）
  productionTime: number;
  // 购买时间 （时间戳）
  purchaseTime: number;
  // 车牌号
  licensePlate?: string;
  // 备注
  notes?: string;
  // 创建时间
  createdAt: string;
  // 更新时间
  updatedAt: string;
  // 车类型 (燃油车、电车、摩托车)
  vehicleType: 'fuel' | 'electric' | 'motor';
  // 当前里程 （公里）
  currentMileage: number;
  // 油箱容积 (升) - 燃油车特有
  fuelCapacity?: number;
  // 燃油车才有此字段，燃料类型 (有：#92，#95，#98，电，其他)
  fuelType?: string;
  // 购买价格
  purchasePrice?: number;
  // 货币
  currency?: string;
  // 电车容量 - 电车特有
  batteryCapacity?: number;
  // 续航里程 - 电车特有
  mileage?: number;
}

// 车辆类型枚举
export enum CarCategory {
  CAR = '小汽车',
  ELECTRIC = '电车',
  MOTORCYCLE = '摩托车',
  TRUCK = '卡车',
  OTHER = '其他',
}

// 车辆类型枚举
export enum VehicleType {
  FUEL = 'fuel',
  ELECTRIC = 'electric',
  MOTOR = 'motor',
}

// 燃油类型枚举
export enum FuelType {
  GAS_92 = '#92',
  GAS_95 = '#95',
  GAS_98 = '#98',
  ELECTRIC = '电',
  OTHER = '其他',
}

// 货币类型
export enum Currency {
  CNY = 'CNY',
  USD = 'USD',
  EUR = 'EUR',
}

// 费用记录相关类型
export interface TFuelInfo {
  /** 表显单价 */
  showPrice: number;
  /** 实付单价 */
  actualPrice?: number;
  /** 表显金额 */
  showAmount: number;
  /** 加油量 */
  fuelAmount: number;
  /** 燃油类型 */
  fuelType?: '#92' | '#95' | '#98' | '柴油' | '电' | '其他';
  /** 优惠金额 */
  discountAmount?: number;
  /** 实付金额 */
  actualAmount?: number;
}

export interface IExpenseStats {
  distance: number; // 行驶里程
  fuelConsumptionPer100km: number; // 百公里油耗
  costPer100km: number; // 百公里费用
  fuelConsumption: number; // 消耗油量
  previousMileage: number; // 上次里程
  currentMileage: number; // 当前里程
}

export interface IExpense {
  _id?: string;
  userId: string;
  vehicleId: string;
  type: ExpenseType;
  /** 里程数 */
  mileage: number;
  /** 加油信息 */
  fuelInfo?: TFuelInfo;
  /** 邮箱是否加满 */
  isFull?: boolean;
  /** 上次是否记录 */
  isRecord?: boolean;
  /** 油箱灯是否亮 */
  isWarning?: boolean;
  /** 货币 */
  currency?: Currency;
  /** 费用日期 */
  date: Date;
  /** 备注 */
  notes?: string;
  /** 费用地点 */
  location?: string;
  /** 里程表（保留向后兼容） */
  odometer?: number;
  /** 描述（保留向后兼容） */
  description?: string;
  imageUrl?: string;
  createdAt?: Date;
  updatedAt?: Date;
  /** 虚拟字段：计算总金额 */
  totalAmount?: number;
  /** 统计信息 */
  stats?: IExpenseStats;
}

// 扩展的Expense类型，包含时间戳和统计信息
export interface IExpenseWithStats
  extends Omit<IExpense, 'createdAt' | 'updatedAt' | 'date'> {
  createdAt?: number | null;
  updatedAt?: number | null;
  date?: number | null;
  createdAtTimestamp?: number | null;
  updatedAtTimestamp?: number | null;
  dateTimestamp?: number | null;
  stats?: IExpenseStats;
}

export enum ExpenseType {
  FUEL = '加油',
  MAINTENANCE = '保养',
  REPAIR = '维修',
  PARKING = '停车费',
  INSPECTION = '年检',
  INSURANCE = '保险',
  TICKET = '罚单',
  TOLL = '过路费',
  TOWING = '拖车费',
  WASHING = '洗车费',
  PARKING_SPACE = '车位费',
}

// 加油记录相关类型
export interface IFuelRecord {
  _id?: string;
  userId: string;
  vehicleId: string;
  date: Date;
  odometer: number;
  fuelType: FuelType;
  unitPrice: number;
  quantity: number;
  totalAmount: number;
  actualUnitPrice?: number;
  discountAmount?: number;
  actualAmount?: number;
  isFullTank: boolean;
  isLowFuelWarning: boolean;
  isLastRecordMissed: boolean;
  notes?: string;
  imageUrl?: string;
  createdAt?: Date;
  updatedAt?: Date;
}

// 充电记录相关类型
export interface IChargingRecord {
  _id?: string;
  userId: string;
  vehicleId: string;
  date: Date;
  odometer: number;
  unitPrice: number;
  quantity: number;
  totalAmount: number;
  actualUnitPrice?: number;
  discountAmount?: number;
  actualAmount?: number;
  isFullCharge: boolean;
  isLowBatteryWarning: boolean;
  isLastRecordMissed: boolean;
  notes?: string;
  imageUrl?: string;
  createdAt?: Date;
  updatedAt?: Date;
}

// 保养记录相关类型
export interface IMaintenanceRecord {
  _id?: string;
  userId: string;
  vehicleId: string;
  date: Date;
  odometer: number;
  type: MaintenanceType;
  laborCost: number;
  partsCost: number;
  totalCost: number;
  description?: string;
  nextMaintenanceOdometer?: number;
  nextMaintenanceDate?: Date;
  createdAt?: Date;
  updatedAt?: Date;
}

export enum MaintenanceType {
  AIR_FILTER = '空气滤清器',
  OIL_FILTER = '机油滤清器',
  FUEL_FILTER = '燃油滤清器',
  CABIN_FILTER = '驾驶室空气过滤器',
  ENGINE_BELT = '发动机皮带',
  TIMING_BELT = '同步带',
  BRAKE_FLUID = '制动液',
  COOLANT = '防冻液',
  TRANSMISSION_OIL = '自动变速箱油',
  FRONT_BRAKE_PADS = '前刹片',
  REAR_BRAKE_PADS = '后刹片',
  OIL_CHANGE = '换油',
  ROTATION_BALANCE = '旋转平衡',
  SPARK_PLUGS = '火花塞',
  WHEEL_ALIGNMENT = '车轮定位',
  WIPER = '雨刷',
}

// 维修记录相关类型
export interface IRepairRecord {
  _id?: string;
  userId: string;
  vehicleId: string;
  date: Date;
  odometer: number;
  type: string;
  laborCost: number;
  partsCost: number;
  totalCost: number;
  description?: string;
  createdAt?: Date;
  updatedAt?: Date;
}

// 收入记录相关类型
export interface IIncome {
  _id?: string;
  userId: string;
  vehicleId: string;
  type: IncomeType;
  amount: number;
  date: Date;
  description?: string;
  createdAt?: Date;
  updatedAt?: Date;
}

export enum IncomeType {
  OPERATION = '运营收入',
  COMMERCIAL = '商业用途收入',
  VEHICLE_SALE = '车辆出售',
  INSURANCE_CLAIM = '保险理赔',
  SUBSIDY = '补贴与优惠',
  OTHER = '其他',
}

// 统计相关类型
export interface IStatistics {
  period: string;
  totalExpense: number;
  totalIncome: number;
  netExpense: number;
  fuelConsumption: number;
  averageFuelConsumption: number;
  totalDistance: number;
  averageDistance: number;
  totalDiscount: number;
  expenseBreakdown: Record<ExpenseType, number>;
}

// API响应类型
export interface ApiResponse<T = any> {
  success: boolean;
  message: string;
  data?: T;
  error?: string;
}

// 分页类型
export interface PaginationParams {
  page: number;
  limit: number;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
}

export interface PaginatedResponse<T> {
  data: T[];
  pagination: {
    page: number;
    limit: number;
    total: number;
    totalPages: number;
  };
}

// JWT Token类型
export interface JwtPayload {
  userId: string;
  username: string;
  role: UserRole;
  iat?: number;
  exp?: number;
}

// 请求上下文类型
export interface RequestContext {
  user?: IUser;
  userId?: string;
}

// 导出API类型
export * from './api';
